using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using UltimateDefend.Properties;
using System.Drawing.Imaging;

namespace UltimateDefend
{
    public partial class SurvivalFrm : Form
    {        
        private Bitmap m_bitmapBuff;    //luu hinh tam de ve
        private bool m_IsBack = false;
        private IMap map;
        //private HeavyArmor h;
        //private int k;
        private ITower m_tower;         //luu tower hien hanh cua nguoi dung dang chon de build

        //luu cac che do dieu khien cua nguoi dung
        private bool isBuild;
        //private bool isSell;
        //private bool isUpgrade;
        ////private bool isMap;

        //luu vi tri nguoi dung dang chon tren man hinh
        private Point m_currPoint;

        //neu nguoi dung dang di chuyen mouse
        private bool isMouseMove;

        //luu tower nguoi dung dang chon       
        private int m_iSelectedTower;   //luu chi so cua tower dang duoc nguoi dung chon
        public SurvivalFrm()
        {
            InitializeComponent();
            //khoi tao bitmap
            
            this.m_bitmapBuff = new Bitmap(this.ClientRectangle.Width, this.ClientRectangle.Height);

            //chi so di chuyen cua quan linh
            //this.k = 0;

            //load ban do len
            map = new MapLevel1();
            map.LoadMap(this.m_bitmapBuff);

            //new 1 doi tuong quan linh de di chuyen
            
            //khoi tao index
            this.m_iSelectedTower = -1; //luc dau chua chon tower nao het
        }

        
        private void SurvivalFrm_Load_1(object sender, EventArgs e)
        {
            //khoi tao cac che do dieu khien
            this.isBuild = false;
            //this.isSell = false;
            //this.isUpgrade = false;
            this.isMouseMove = false;
        }

        private Point pAccept;               
        protected override void OnPaint(PaintEventArgs e)
        {
            if (isMouseMove)
            {
                Bitmap bmpTmp = new Bitmap(this.m_bitmapBuff);
                
                if (this.m_tower.CanBuild(map, this.m_currPoint, 1000))
                {
                    this.m_tower.DrawTower(bmpTmp, true, Color.Green, this.m_currPoint);                                      
                }
                else
                {
                    this.m_tower.DrawTower(bmpTmp, true, Color.Yellow, this.m_currPoint);
                }

                //ve man hinh
                e.Graphics.DrawImage(bmpTmp, 0, 0);

                //huy bien bitmap tam
                bmpTmp.Dispose();
            }
            else
            {               
                
                //ve len tower dang nguoi nguoi dung chon
                if (this.m_iSelectedTower != -1)
                {
                    map.SeletedTower(this.m_iSelectedTower);                    
                }
                map.RefreshMap(this.m_bitmapBuff);
                //ve man hinh
                e.Graphics.DrawImage(this.m_bitmapBuff, 0, 0);
            }            
        }

        protected override void OnPaintBackground(PaintEventArgs e)
        {
            if (!this.m_IsBack)
            {
                this.m_IsBack = true;
            }
        }                   
               

        private void timer1_Tick(object sender, EventArgs e)
        {
        }

        private void SurvivalFrm_MouseDown_1(object sender, MouseEventArgs e)
        {           
            //neu dang che do build thi chon vi tri de build
            //if (isBuild)
            //{
            //    ITower tmpTower = this.m_tower;
                
            //    //build tower tai vi tri e.x va e.y
            //    if (tmpTower.Build(map, this.m_bitmapBuff, new Point(e.X, e.Y), 1000) > 0)
            //    {
            //        //them tower moi vao ban do
            //        map.AddTower(tmpTower);

            //        //ve lai ban do
            //        this.Invalidate();
            //    }
                
            //}

            //neu dang che do sell thi chon tower de sell
            //if (isSell)
            //{
            //    this.m_currPoint.X = e.X;
            //    this.m_currPoint.Y = e.Y;
            //    int index = map.IsInListTower(this.m_currPoint);
            //    if (index != -1)
            //    {
            //        map.RemoveTower(index);
            //    }
            //    this.Invalidate();
            //}

            ////neu dang che do upgrade thi chon tower de upgrade
            //if (isUpgrade)
            //{
            //}            

            this.m_currPoint.X = e.X;
            this.m_currPoint.Y = e.Y;
            this.m_iSelectedTower = map.IsInListTower(this.m_currPoint);
            this.Invalidate();
            //if (index != -1)
            //{
            //    map.ListTower[index].SelectedTower(this.m_bitmapBuff);
            //}
        }

        private void btnBuild_Click(object sender, EventArgs e)
        {
            //ngung hoat dong cua quan linh lai
            timer1.Enabled = false;

            //this.isUpgrade = false;
            this.isBuild = true;
            //this.isSell = false;
            //this.isMap = false;

            //show dialog build
            BuildFrm frm = new BuildFrm();
            frm.ShowDialog();

            //lay ra tower nguoi dung dang chon
            this.m_tower = frm.Tower;

            //load len man hinh cho nguoi dung biet nhung vi tri co the xay tower            
            Graphics g = Graphics.FromImage(this.m_bitmapBuff);

                //ve lai man hinh grass luc dau khi chua co tree
            g.DrawImage(map.BitmapMap, new Rectangle(0, 0, ClientRectangle.Width, ClientRectangle.Height), new Rectangle(0, 0, map.BitmapMap.Width, map.BitmapMap.Height), GraphicsUnit.Pixel);
            
            //ve lai ban do (co danh dau cac vi tri co the build tower)
            map.RefreshMap(this.m_bitmapBuff);

            //huy graphics
            g.Dispose();

            //ve lai man hinh
            this.Invalidate();
        }

        private void btnSell_Click(object sender, EventArgs e)
        {
            //cho quan linh hoat dong lai
            timer1.Enabled = true;

            //this.isUpgrade = false;
            this.isBuild = false;
            //this.isSell = true;
            //this.isMap = false;

            //khong xet move move
            this.isMouseMove = false;

            //ban tower
            if (this.m_iSelectedTower != -1)
            {
                map.RemoveTower(this.m_iSelectedTower);
                this.m_iSelectedTower = -1;
                this.Invalidate();
            }
        }
       
        private void btnUpgrade_Click(object sender, EventArgs e)
        {
            //cho quan linh ngu hoat dong
            timer1.Enabled = false;

            //this.isUpgrade = true;
            this.isBuild = false;
            //this.isSell = false;
            //this.isMap = false;

            this.isMouseMove = false;

            //show dialog upgrade
            BuildFrm frm = new BuildFrm();
            frm.ShowDialog();

            //ve hien thi len tower level moi
            if (this.m_iSelectedTower != -1)
            {
                ITower tower = frm.Tower;
                //luu lai vi tri cua tower cu cho tower moi
                tower.Position = map.ListTower[this.m_iSelectedTower].Position;

                //loai bo tower cu
                map.RemoveTower(this.m_iSelectedTower);
                //them vao tower moi tai vi tri cu
                map.AddTower(tower);

                this.m_iSelectedTower = -1;
                this.Invalidate();
            }
        }

        
        private void SurvivalFrm_MouseMove(object sender, MouseEventArgs e)
        {
            //chi xu ly khi dang build
            if (isBuild)
            {
                //nguoi dung dang di chuyen mouse
                this.isMouseMove = true;

                //luu lai diem nguoi dung dang di chuyen mouse
                this.m_currPoint = new Point(e.X, e.Y);

                //ve lai man hinh
                this.Invalidate();
            }
        }

        private void btnMap_Click(object sender, EventArgs e)
        {
            if (isBuild)            
            {              

                //cho quan linh hoat dong
                timer1.Enabled = true;

                //this.isUpgrade = false;
                this.isBuild = false;
                //this.isSell = false;
                //this.isMap = true;

                this.isMouseMove = false;

                //ve tower tai day va add tower vao                             

                ITower tmpTower = this.m_tower;

                //build tower tai vi tri pAccept
                if (tmpTower.Build(map, this.m_bitmapBuff, pAccept, 1000) > 0)
                {
                    //them tower moi vao ban do
                    map.AddTower(tmpTower);                
                }                                             

                //ve lai man hinh
                this.Invalidate();

                this.isBuild = false;
            }
        }

        private void SurvivalFrm_MouseUp(object sender, MouseEventArgs e)
        {
            //chi xet khi dang build tower
            if (isBuild)
            {
                if (this.m_tower.CanBuild(map, this.m_currPoint, 1000))
                {
                    btnMap.Visible = true;

                    //luu lai vi tri co the build tower
                    pAccept = this.m_currPoint;                    
                }
                else
                {
                    btnMap.Visible = false;
                }
            }
        }       
    }
}